package com.fancon.rage.comic;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import com.fancon.rage.comic.entity.Photo;
import com.google.appengine.api.datastore.EntityNotFoundException;

@SuppressWarnings("serial")
public class RageComicSevletEn extends AbstractUploadServlet {

    @Override
    protected void showForm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    List<Photo> enComics = dao.listByProperty("countryCode", "en", 50, 0);
        for (Photo photo : enComics) {
			photo.setTitle(URLDecoder.decode(photo.getTitle(), "UTF-8"));
		}
        req.setAttribute("comics", enComics);
        req.setAttribute("countryCode", "en");
        req.setAttribute("root", "ragecomicen");
    	req.getRequestDispatcher("ragecomic.jsp").forward(req, resp);        
    }

    @Override
    protected void handleSubmit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletFileUpload upload = new ServletFileUpload();

        try {
            FileItemIterator it = upload.getItemIterator(req);

            Photo photo = new Photo();

            while (it.hasNext()) {
                FileItemStream item = it.next();
                String fieldName = item.getFieldName();
                InputStream fieldValue = item.openStream();

                if ("title".equals(fieldName)) {
                    photo.setTitle(URLEncoder.encode(Streams.asString(fieldValue, "UTF-8"), "UTF-8"));
                    continue;
                }

                if ("thumbFile".equals(fieldName)) {
                    photo.setContentType(item.getContentType());
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    Streams.copy(fieldValue, out, true);
                    photo.setThumbData(out.toByteArray());
                    continue;
                }

                if ("imageFile".equals(fieldName)) {
                    photo.setContentType(item.getContentType());
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    Streams.copy(fieldValue, out, true);
                    photo.setPhotoData(out.toByteArray());
                    continue;
                }
            }

            photo.setCountryCode("en");
            photo.setDate(new Date());
            dao.add(photo);
            resp.sendRedirect("ragecomicen");            
        } catch (FileUploadException e) {
            throw new ServletException(e);
        }        
    }

	@Override
	protected void delete(long id, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		Photo photo = null;
		try {
			photo = dao.get(id);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dao.delete(photo);
		resp.sendRedirect("ragecomicen");    
//	    List<Photo> vnComics = dao.listByProperty("countryCode", "en");
//        req.setAttribute("comics", vnComics);
//        req.setAttribute("countryCode", "en");
//    	req.getRequestDispatcher("ragecomic.jsp").forward(req, resp);
	}

}
